<?
if($_POST[action]=="save"){
$fileupload=$_FILES['fileupload']['tmp_name'];
$fileupload_name=$_FILES['fileupload']['name'];
$fileupload_size=$_FILES['fileupload']['size'];
$fileupload_type=$_FILES['fileupload']['type'];
	if($fileupload){
			$sql = "delete from tb_detail";
			$qr = mysql_query($sql);
			$sql = "ALTER TABLE  `tb_detail` AUTO_INCREMENT =1";
			$qr = mysql_query($sql);
			require_once 'Classes/PHPExcel/IOFactory.php';
			$objPHPExcel = PHPExcel_IOFactory::load($fileupload);
			foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
                $worksheetTitle     = $worksheet->getTitle();
                $highestRow         = $worksheet->getHighestRow(); // e.g. 10
                $highestColumn      = $worksheet->getHighestColumn(); // e.g 'F'
                $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
                $nrColumns = ord($highestColumn) - 64;
				
                for ($row = 2; $row <= $highestRow; ++ $row) {
				
                        $cell = $worksheet->getCellByColumnAndRow(0, $row);
                        $value = $cell->getValue();
						
						$cell1 = $worksheet->getCellByColumnAndRow(1, $row);
                        $value1 = $cell1->getValue();
						
						$cell2 = $worksheet->getCellByColumnAndRow(2, $row);
                        $value2 = $cell2->getValue();
						
						$cell3 = $worksheet->getCellByColumnAndRow(3, $row);
                        $value3 = $cell3->getValue();
						
						$cell4 = $worksheet->getCellByColumnAndRow(4, $row);
                        $value4 = $cell4->getValue();
						
						$cell5 = $worksheet->getCellByColumnAndRow(5, $row);
                        $value5 = $cell5->getValue();
						
						$cell6 = $worksheet->getCellByColumnAndRow(6, $row);
                        $value6= $cell6->getValue();
						
						$cell7 = $worksheet->getCellByColumnAndRow(7, $row);
                        $value7 = $cell7->getValue();
						
						//insert data//
						$data = array(
						"country"=>sqlEscape($value),
						"city"=>sqlEscape($value1),
						"institution"=>sqlEscape($value2),
						"certificate"=>sqlEscape($value3),
						"major"=>sqlEscape($value4),
						"course_name"=>sqlEscape($value5),
						"durations"=>sqlEscape($value6),
						"fee"=>sqlEscape($value7),
						"note"=>sqlEscape($value8),
						);
						
						insert("tb_detail",$data);
						//end insert//
				}
			}
	
	}	
	?>					
	<SCRIPT LANGUAGE="JavaScript">alert("update content suscessfully");window.location='admin.php?mode=detail_list.php';</script>						
	<?php					
}//end $_POST[action]//

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="15" align="left" class="headmenu"><img src="image/bar_left.gif" width="15" height="42" /></td>
    <td align="left" bgcolor="#434343" class="headmenu">ข้อมูลสถานศึกษา-&gt;อัพเดทข้อมูลใหม่ทั้งหมด</td>
    <td width="15" align="left" class="headmenu"><img src="image/bar_right.gif" width="15" height="42" /></td>
  </tr>
</table>

<form action="admin.php?mode=data.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
  <table style="margin-top:20px;" width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
    <tr class="subject">
      <td width="20%" align="center" bgcolor="#FFFFFF" class="menu">ไฟล์ Excel</td>
      <td width="72%" align="left" bgcolor="#FFFFFF"><label>
        <input name="fileupload" type="file" id="fileupload" />
      </label></td>
    </tr>
    <tr>
      <td colspan="2" align="left" bgcolor="#FFFFFF"><label></label>
        <label>
        <input type="submit" name="button" id="button" value="Submit" onClick="return confirm('ต้องการจะปรับปรุงข้อมูลใหม่?')"/>
        <input name="action" type="hidden" id="action" value="save" />
      </label></td>
    </tr>
  </table>
</form>
</body>
</html>
